package com.lys.enchantment;

import com.lys.enchantment.armor.PerseveranceEnchantment;
import com.lys.enchantment.bow.ExplosiveArrowEnchantment;
import com.lys.enchantment.bow.MultiArrowEnchantment;
import com.lys.enchantment.bow.QuickPullingEnchantment;
import com.lys.enchantment.weapon.BloodthirstySwordEnchantment;
import com.lys.enchantment.weapon.LightningAxeEnchantment;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.EnchantedBookItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.text.Text;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class EnchantedBookTooltipCallback {
    public static void addTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip) {
        NbtCompound nbt = stack.getNbt();
        NbtList enchantments=null;
        if (stack.getItem() instanceof EnchantedBookItem){
            assert nbt != null;
            NbtElement storedEnchantments = nbt.get("StoredEnchantments");
            enchantments = (NbtList)storedEnchantments;
        }else {
            enchantments = stack.getEnchantments();
        }
        if (enchantments != null) {
            for (int i = 0; i < Objects.requireNonNull(enchantments).size(); i++) {
                NbtCompound compound = enchantments.getCompound(i);
                NbtElement id = compound.get("id");
                assert id != null;
                Optional.ofNullable(id.asString())
                        .filter(s -> s.equals("moreenchantmentmod:explosive_arrow"))
                        .ifPresent(s -> ExplosiveArrowEnchantment.appendTooltip(tooltip));

                Optional.ofNullable(id.asString())
                        .filter(s -> s.equals("moreenchantmentmod:multi_arrow"))
                        .ifPresent(s -> MultiArrowEnchantment.appendTooltip(tooltip));

                Optional.ofNullable(id.asString())
                        .filter(s -> s.equals("moreenchantmentmod:quick_pulling"))
                        .ifPresent(s -> QuickPullingEnchantment.appendTooltip(tooltip));

                Optional.ofNullable(id.asString())
                        .filter(s -> s.equals("moreenchantmentmod:perseverance"))
                        .ifPresent(s -> PerseveranceEnchantment.appendTooltip(tooltip));

                Optional.ofNullable(id.asString())
                        .filter(s -> s.equals("moreenchantmentmod:bloodthirsty_sword"))
                        .ifPresent(s -> BloodthirstySwordEnchantment.appendTooltip(tooltip));

                Optional.ofNullable(id.asString())
                        .filter(s -> s.equals("moreenchantmentmod:lightning_axe"))
                        .ifPresent(s -> LightningAxeEnchantment.appendTooltip(tooltip));
            }
        }
    }
}
